home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’91 / Help for Hier / JKMenuSelect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-20  |  7.8 KB  |  321 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3. #include <values.h>
  4. #include <types.h>
  5. #include <quickdraw.h>
  6. #include <fonts.h>
  7. #include <events.h>
  8. #include <controls.h>
  9. #include <windows.h>
  10. #include <menus.h>
  11. #include <textedit.h>
  12. #include <dialogs.h>
  13. #include <desk.h>
  14. #include <scrap.h>
  15. #include <toolutils.h>
  16. #include <memory.h>
  17. #include <segload.h>
  18. #include <files.h>
  19. #include <osutils.h>
  20. #include <osevents.h>
  21. #include <diskinit.h>
  22. #include <packages.h>
  23. #include <traps.h>
  24.  
  25.  
  26. #include    <ctype.h>
  27. #include    <fcntl.h>
  28. #include    <string.h>
  29. #include    <stdio.h>
  30. #include    <ErrMgr.h>
  31. #include    <CursorCtl.h>
  32. #include    <Errors.h>
  33.  
  34. #include <Devices.h>
  35.  
  36. */
  37.  
  38.  
  39. #define NIL_POINTER        0L
  40. #define MOVE_TO_FRONT     -1L
  41. #define REMOVE_ALL_EVENTS  0
  42. #define VISIBLE               0
  43.  
  44.  
  45. #define PLAIN              0
  46. #define PLAIN_ITEM         1
  47. #define BOLD_ITEM          2
  48. #define ITALIC_ITEM        3
  49. #define UNDERLINE_ITEM     4
  50. #define OUTLINE_ITEM       5
  51. #define SHADOW_ITEM        6
  52.  
  53. #define FALSE             (0)
  54. #define TRUE              (!FALSE)
  55.  
  56. #define INCLUDE_SECONDS    TRUE
  57.  
  58. #define ADD_CHECK_MARK     TRUE
  59. #define REMOVE_CHECK_MARK  FALSE
  60.  
  61. #define DRAG_THRESHOLD     30
  62.  
  63. #define MIN_SLEEP          0L
  64. #define NIL_MOUSE_REGION   0L
  65.  
  66. #define WNE_TRAP_NUM       0x60
  67. #define UNIMPL_TRAP_NUM    0x9F
  68.  
  69. #define QUIT_ITEM          1
  70. #define ABOUT_ITEM         1
  71.  
  72. #define NOT_A_NORMAL_MENU  -1
  73.  
  74. #define ABOUT_ALERT        400
  75.  
  76. #define HIER_MENU_CHAR      '\x1b'
  77.  
  78. typedef struct MenuItemRecord{char menuItemString[90];  /*Trouble if string is over 90*/
  79.                  int menuItemCommand;
  80.                  int menuItemMark;
  81.                  int menuItemStyle;
  82.                 }MenuItem;
  83.  
  84.  
  85. long allMenuChoice(void);
  86. void figureMenuRectSize(MenuHandle aMenuH, MenuHandle aHMenuH, Rect *menuRect);
  87. void DrawMenuWindow(int menuItemCount,MenuItem **pickListH,int *lineHeightPtr);
  88. pascal long main( Point thePoint );
  89. void HandleJKMouseDown( Point    clickPoint, long *resultPtr, int hMenuID,
  90.                         int lineHeight,WindowPtr menuWindow);
  91. char    specialMenu(MenuHandle theMenuH);
  92.  
  93. /*********************** Main *********/
  94.  
  95. /***************  MenuSelect( gTheEvent.where )  ************/
  96.  
  97. pascal long main( Point thePoint )
  98. {
  99.     long            oldMenuSelectResult, deadMenuChoice, temp;
  100.     char            theChar;
  101.     Boolean            done;
  102.     EventRecord        jkMenuEvent;
  103.     int                theMenu, theItem, lineHeight;
  104.     MenuHandle        theMenuH, theHMenuH;
  105.     long int        ignoreThis;
  106.     short            deadCmdMark, deadCmdChar, menuItemCount, i;
  107.     MenuItem        **pickListH;
  108.     Rect            menuRect;
  109.     WindowPtr        menuWindow;
  110.     
  111.     void (*menuHandleProc) ();
  112.  
  113.     long *start;
  114.  
  115.     asm
  116.     {
  117.         move.l a0, start
  118.     }
  119.  
  120.     oldMenuSelectResult = CallPascalL(thePoint,start[3]);
  121.  
  122.     if (!oldMenuSelectResult) 
  123.     {
  124.         deadMenuChoice = allMenuChoice();
  125.         theMenu = HiWord( deadMenuChoice );
  126.         if (theItem = LoWord( deadMenuChoice ))
  127.         {
  128.             theMenuH = GetMHandle(theMenu);
  129.             GetItemMark (theMenuH, theItem, &deadCmdMark);
  130.             GetItemCmd (theMenuH, theItem, &deadCmdChar);
  131.             theHMenuH = GetMHandle(deadCmdMark);
  132.             if ((deadCmdChar == '\x1b'))/*&&
  133.                                     (!specialMenu(theHMenuH = GetMHandle(deadCmdMark))))
  134. */            {
  135.                 HLock(theHMenuH);
  136.                 figureMenuRectSize(theMenuH,theHMenuH,&menuRect);
  137. /*                menuItemCount = CountMItems(theHMenuH);
  138.                 pickListH = (MenuItem **)NewHandle( 
  139.                              sizeof(struct MenuItemRecord)*menuItemCount );
  140. */
  141.                 menuWindow = NewWindow(    (void *)NIL_POINTER,
  142.                                         (Rect *)&menuRect,
  143.                                         (ConstStr255Param)"\pTest",
  144.                                         (Boolean)!VISIBLE,
  145.                                         (short)altDBoxProc,
  146.                                         (WindowPtr)MOVE_TO_FRONT,
  147.                                         (Boolean)FALSE,
  148.                                         (long)'\xF0');
  149.                 SetPort( menuWindow );
  150.  
  151.                 menuHandleProc = (void (*) ())(*((*theHMenuH)->menuProc));
  152.  
  153.                 i = 0;
  154.                 
  155.                 GlobalToLocal((Point *)&menuRect.bottom);
  156.                 menuRect.top = -20;
  157.                 menuRect.left = 0;
  158.                 CallPascal((short)mDrawMsg,theHMenuH,&menuRect,thePoint,&i,
  159.                             menuHandleProc);
  160.  
  161.                 done = FALSE;
  162.                 while (!done)
  163.                 {
  164.                     if (GetNextEvent( everyEvent, /*mUpMask+keyDownMask,*/ &jkMenuEvent ))
  165.                         switch ( jkMenuEvent.what)
  166.                         {
  167.                               case mouseDown:
  168.                                       i=0;
  169.                                       GlobalToLocal(&jkMenuEvent.where);
  170.                                       while (Button())
  171.                                       {
  172.                                       GetMouse(&thePoint);
  173.                                     CallPascal((short)mChooseMsg,theHMenuH,&menuRect,
  174.                                         thePoint,&i,menuHandleProc);
  175.                                     }
  176.                                     temp = (**theHMenuH).menuID;
  177.                                     temp <<= 16;
  178.                                     temp += i;
  179.                                     if (i == 0)
  180.                                         oldMenuSelectResult = 0;
  181.                                     else
  182.                                         oldMenuSelectResult = temp;
  183.                                     
  184. /*                                   HandleJKMouseDown(jkMenuEvent.where, &oldMenuSelectResult,
  185.                                            (**theHMenuH).menuID,lineHeight,menuWindow);            
  186. */
  187.  
  188.                                            done = !FALSE;
  189.                                    break;
  190.                               case keyDown:
  191.                               case autoKey:
  192.                                     theChar = jkMenuEvent.message & charCodeMask;
  193.                                     if (theChar== 0x1B)
  194.                                         done = !FALSE;
  195.                                     break;
  196.                               case activateEvt:
  197.                                       if ((jkMenuEvent.message == (long)menuWindow)&&
  198.                                           (!(activeFlag&jkMenuEvent.modifiers)))
  199.                                     {
  200.                                         done = !FALSE;
  201.                                     }
  202.                                     break;
  203.                               
  204.                         }
  205.                 }
  206.                    DisposeWindow( menuWindow );
  207. /*                   DisposHandle( (Handle)pickListH );
  208. */
  209.                    HUnlock(theHMenuH);
  210.             }
  211.         }
  212.     };
  213.     return oldMenuSelectResult;
  214.     
  215. }
  216. char    specialMenu(MenuHandle theMenuH)
  217. {
  218.     short    menuIdNumber;
  219.     Str255    menuStringTitle;
  220.  
  221.     menuIdNumber = 3333;
  222.     menuStringTitle[0] = 0;
  223.     return (*(**NewMenu(menuIdNumber,menuStringTitle)).menuProc)
  224.                 !=(*(**theMenuH).menuProc);
  225. }
  226.  
  227. /*********************** HandleJKMouseDown *********/
  228.  
  229. void HandleJKMouseDown(    Point clickPoint, long *resultPtr, int hMenuID,
  230.                         int lineHeight, WindowPtr menuWindow)
  231. {
  232.     WindowPtr   whichWindow;
  233.     short int   thePart;
  234.     long int    menuSelChoice;
  235.     int            tempItem;
  236.  
  237.     Debugger();
  238.     thePart = FindWindow( clickPoint, &whichWindow );
  239.     if (whichWindow != menuWindow)
  240.         *resultPtr = 0;
  241.     else
  242.         {
  243.             SetPort(menuWindow);
  244.             GlobalToLocal(&clickPoint);
  245.             tempItem = clickPoint.v/lineHeight;
  246.             *resultPtr = hMenuID;
  247.             *resultPtr <<= 16;
  248.             *resultPtr += ++tempItem;
  249.         }
  250. }
  251. /*********************** figureMenuRectSize *********/
  252. void figureMenuRectSize(MenuHandle aMenuH, MenuHandle aHMenuH,Rect *menuRect)
  253. {
  254.     short          *tempPtr,i;
  255.  
  256.       menuRect->top    = 0;
  257.     /*get left edge of menu in menu bar*/
  258.     menuRect->left   = 0;
  259.     tempPtr = (*(int **)MenuList);
  260.     tempPtr+=3;
  261.     for (i=1;i<=((int)**(int **)MenuList)/6;++i&&!menuRect->left,tempPtr+=3)
  262.         {
  263.         if (*(MenuHandle *)tempPtr == aMenuH)
  264.            menuRect->left = tempPtr[2];
  265.         };
  266.     menuRect->right  = menuRect->left + (*aHMenuH)->menuWidth+20;
  267.     menuRect->bottom = menuRect->top  + (*aHMenuH)->menuHeight+20;
  268.     
  269. /*    if (menuRect->right > screenBits.bounds.right)
  270.         {
  271.             menuRect->right = screenBits.bounds.right;
  272.             menuRect->left = menuRect->right - (*aHMenuH)->menuWidth;
  273.         };
  274.     if (menuRect->bottom > screenBits.bounds.bottom)
  275.         {
  276.             menuRect->bottom = screenBits.bounds.bottom;
  277.         };
  278. */
  279. }
  280.  
  281. /*************   DrawMenuWindow   *************************/
  282.  
  283. void DrawMenuWindow(int menuItemCount,MenuItem **pickListH,int *lineHeightPtr)
  284. {
  285.     FontInfo   fontStuff;
  286.     int           insetSpace, i;
  287.  
  288.  
  289.        TextFont(0);
  290.        TextSize(0);
  291.        GetFontInfo(&fontStuff);
  292.        insetSpace = fontStuff.widMax+1;
  293.        MoveTo(1,fontStuff.ascent+1);
  294.        *lineHeightPtr =fontStuff.ascent+fontStuff.descent+fontStuff.leading;
  295.        for(i = 0 ;i<=menuItemCount;)
  296.        {
  297.            if ((((*pickListH)[i]).menuItemString)[1]=='-') /*SysBeep(30)*/;
  298.            if ( ((*pickListH)[i]).menuItemMark )
  299.                DrawChar(((*pickListH)[i]).menuItemMark);
  300.            MoveTo(insetSpace,fontStuff.ascent+1+((i)*(*lineHeightPtr)));
  301.            TextFace(((*pickListH)[i]).menuItemStyle);
  302.            DrawString(&((*pickListH)[i]).menuItemString);
  303.            TextFace(0);
  304.            MoveTo(1,fontStuff.ascent+1+(++i*(*lineHeightPtr)));
  305.        }
  306.  
  307. }
  308.  
  309. /*********************** allMenuChoice *********/
  310.  
  311. long int allMenuChoice(void)
  312. /* This simulates MenuChoice, but works for all machines */
  313. {
  314.     struct SysEnvRec theWorld;
  315.     SysEnvirons(1,&theWorld);
  316.     return (theWorld.machineType>0)&&(theWorld.machineType<4)?
  317.            MenuDisable:
  318.            MenuChoice();
  319. }
  320.  
  321.